<?php
class tbl_admin_member extends AppModel
{
    public $useTable = 'admin_member';
    public $useDbConfig = 'fantasy_hero_kr_log';
    public $primaryKey = '_id';

    public $validate = array(
        'admin_id' => array(
            'custom_rule' => array(
                'rule' => 'alphaNumeric',
                'message' => 'No Special Marks'
            ),
            'length' => array(
                'rule' => array('minLength', 4),
                'message' => 'Minimum length of 4 characters'
            ),
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            )
        ),
        'admin_pass' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            )
        ),
        'admin_pass_conf' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            )
        ),
        'admin_name' => array(
            'required' => array(
                'rule' => array('notEmpty')
            )
        ),
        'admin_belong' => array(
            'required' => array(
                'rule' => array('notEmpty')
            )
        ),
    );

    public function admin_login($_id, $_pass)
    {
        $params = array(
            'conditions' => array(
                'admin_id' => $_id
            ),
            'fields' => array(
                'admin_auth', 'admin_id', 'admin_pass', 'admin_name', 'admin_belong'
            ),
        );

        $data = $this->find('first', $params);

        if(!empty($data))
        {
            echo(md5($_pass));
            if(strtolower(md5($_pass)) != $data['tbl_admin_member']['admin_pass'])
            {
                return false;
            }
        }

        return $data;
    }

    public function get_admin_by_id($_id)
    {
        $params = array(
            'conditions' => array('admin_id' => $_id)
        );

        $data = $this->find('first', $params);

        return $data;
    }

    public function get_admin_list($_id = null)
    {
        $conditions = '';

        /*if($_id != 'administrator')
        {
            $conditions = array('NOT' => array('admin_id' => 'administrator'));
        }*/

        $params = array(
            'conditions' => $conditions,
            'order' => array(
                'num' => 'ASC'
            ),
        );

        $data = $this->find('all', $params);

        return $data;
    }

    public function admin_id_check($_admin_id)
    {
        $params = array(
            'conditions' => array('admin_id' => $_admin_id)
        );

        $data = $this->find('first', $params);

        return $data;
    }
}